<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field['options'].displayStyle"
  >
    <div class="scene-head-widget" :style="getStyle('head')">
      <template v-if="true">
        <div class="left-con left-wrp item" :style="getStyle('leftcon')">
          <div class="logo-wrap" :style="getStyle('pictureWrp')">
            <template
              v-if="
                optionsInfo?.logoVisit &&
                (!!optionsInfo?.imageUrl?.val || optionsInfo?.imageUrl)
              "
            >
              <img
                class="logo"
                :style="getStyle('picture')"
                :src="pictureResource"
                alt="企业Logo"
              />
            </template>
          </div>
          <div class="adreess" v-if="optionsInfo?.tipTextShow">
            <div :style="getStyle('tipText')">
              {{ optionsInfo.content }}
            </div>
          </div>
        </div>
        <template v-if="optionsInfo.contentShow">
          <!--           v-if="!!(optionsInfo.content || headTitle)"-->
          <div
            class="title item"
            :style="getStyle('title')"
            :data-content="optionsInfo.content || headTitle"
          >
            <NoticeBar
              v-if="optionsInfo.content || headTitle"
              :text="optionsInfo.content || headTitle"
              :style="deptStyle"
              :class="optionsInfo.contentLocation"
            />
            <div v-else :style="deptStyle" :class="optionsInfo.contentLocation">
              {{ getRoomDeptInfo[optionsInfo["deptNameKey"]] || "\u00a0" }}
            </div>
          </div>
        </template>
        <template v-if="true">
          <template v-if="optionsInfo['viewMode'] === 2">
            <div class="item viewMode-2">
              <div class="time view-item">{{ time }}</div>
              <template v-if="optionsInfo.dateShow">
                <div class="week view-item">{{ getWeekInfo(week) }}</div>
                <div class="date view-item">{{ date }}</div>
              </template>
            </div>
          </template>

          <template v-else-if="optionsInfo['viewMode'] === 3">
            <div class="item viewMode-3">
              <template v-if="optionsInfo.dateShow">
                <div class="week view-item">{{ getWeekInfo(week) }}</div>
                <div class="date view-item">{{ date }}</div>
              </template>
              <div class="time view-item">{{ time }}</div>
            </div>
          </template>

          <template v-else-if="optionsInfo['viewMode'] === 4">
            <div
              class="item viewMode-4"
              :class="timeClassName(optionsInfo.changePosition)"
            >
              <template v-if="optionsInfo.dateShow">
                <div class="date view-item">{{ date }}</div>
                <div class="week view-item">{{ getWeekInfo(week) }}</div>
              </template>
              <div class="time view-item">{{ time }}</div>
            </div>
          </template>

          <template v-else-if="optionsInfo['viewMode'] === 5">
            <div class="item viewMode-5">
              <div class="right-top-area view-item">{{ date }}</div>
              <div
                class="right-bottom-area view-item"
                v-if="optionsInfo.dateShow"
              >
                <div class="week">{{ getWeekInfo(week) }}</div>
                <div class="date">{{ time }}</div>
              </div>
            </div>
          </template>
          <template v-else-if="optionsInfo['viewMode'] === 6">
            <div class="item viewMode-6">
              <div class="sec" v-if="optionsInfo.dateShow">
                <div class="week">{{ getWeekInfo(week) }}</div>
                <div class="date">{{ date }}</div>
              </div>
              <div class="time" :style="getStyle('viewMode-1-Date')">
                {{ time }}
              </div>
            </div>
          </template>
          <template v-else-if="optionsInfo['viewMode'] === 7">
            <div class="item viewMode-7" v-if="optionsInfo.viewMode7Show">
              <div class="title">
                <!-- <div
                    class="title item"
                    :style="getStyle('title')"
                    :data-content="optionsInfo.titleContent"
                >
                  <div v-if="optionsInfo.titleContent" :style="deptStyle">
                    {{optionsInfo.titleContent}}
                  </div>
                  <div v-else :style="deptStyle">
                    <template v-for="(groupItem) in waitingPatientSummaryInfos">
                        <ScrollX>
                          {{ groupItem.deptName || getRoomDeptInfo.deptName || "\u00a0" }}
                        </ScrollX>
                    </template>
                  </div>
                </div> -->
                <div
                  class="title item"
                  :style="deptStyle"
                  :data-content="optionsInfo.content || headTitle"
                >
                 {{ optionsInfo.content }}
                </div>
              </div>
            </div>
          </template>
          <template v-else-if="optionsInfo['viewMode'] === 8">
            <div class="viewMode-8">
              <div class="timeCenter" :style="getStyle('viewMode-8-Date')">
                {{ time }}
              </div>
            </div>
          </template>
          <template v-else>
            <div
              class="item viewMode-1"
              :class="timeClassName(optionsInfo.changePosition)"
            >
              <div class="time" :style="getStyle('viewMode-1-Date')">
                {{ time }}
              </div>
              <div class="sec" v-if="optionsInfo.dateShow">
                <div class="week">{{ getWeekInfo(week) }}</div>
                <div class="date">{{ date }}</div>
              </div>
            </div>
          </template>
        </template>
      </template>
    </div>
  </CompOperate>
</template>

<script>
import dayjs from "dayjs";

import { NoticeBar } from "vant";
import emitter from "@/utils/designer/emitter";
import { getWeek, pxToVw } from "@/utils/common";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import { buildParentApi } from "@/utils/designer/util";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import Template from "@sc/template/index.vue";

let buildParent = buildParentApi();

export default {
  name: "scene-head-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    Template,
    ScrollX,
    CompOperate,
    NoticeBar,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1,
    },

    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },

    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },

    visibleTitle: {
      type: Boolean,
    },

    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi(),
    },
    widgetKey: {
      type: String,
    },
  },

  data() {
    return {
      week: "",
      split: 0.5,
      split2: 0.5,
      optionsInfo: Object(),
      mockOpen: false,
      activation: false,
      date: dayjs().format("YYYY年MM月DD日"),
      time: dayjs().format("HH:mm"),
      leftOffset: 30,
      triggerDragging: false,
    };
  },

  computed: {
    timeClassName() {
      return function (val) {
        return val ? "date-time" : "time-date";
      };
    },

    deptStyle() {
      return {
        fontWeight: this.optionsInfo.deptFontWeight,
        color: this.optionsInfo.deptFontColor,
        fontSize: pxToVw(this.optionsInfo.deptFontSize),
      };
    },

    timeType() {
      return this.optionsInfo.timeValue;
    },

    // 获取日期
    getWeekInfo() {
      return function (info) {
        return getWeek(info);
      };
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.optionsInfo || Array();
        let Obj = Object();

        switch (val) {
          case "leftcon":
            Reflect.set(Obj, "height", pxToVw(info.headHeight));
            break;
          case "picture":
            Reflect.set(Obj, "height", `${pxToVw(info.imageHeight)}`);
            Reflect.set(Obj, "width", pxToVw(info.imageWidth));
            Reflect.set(Obj, "borderRadius", pxToVw(info.imageRadius));
            break;
          case "tipText":
            Reflect.set(Obj, "fontSize", pxToVw(38));
            Reflect.set(Obj, "marginLeft", pxToVw(129));
            Reflect.set(Obj, "color", "#333333");
            Reflect.set(Obj, "fontWeight", "500");
            break;
          case "title":
            Reflect.set(Obj, "fontSize", pxToVw(info.fontSize));
            if (info.viewMode === 6) {
              const { deptAreaUrl = [] } = this.optionsInfo;
              if (deptAreaUrl.length > 0) {
                Reflect.set(
                  Obj,
                  "backgroundImage",
                  `url(${deptAreaUrl[0]["url"] || ""})`
                );
              }
            }
            break;
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.headHeight));
            Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            Reflect.set(Obj, "color", `${info.fontColor}`);
            break;
          case "split":
            if (info?.contentShow) {
              Reflect.set(Obj, "flex", 3);
            } else {
              Reflect.set(Obj, "flex", 1);
            }
            break;
          case "viewMode-1-Date":
            Reflect.set(Obj, "backgroundColor", `${info.dataHoursMinBack}`);
            Reflect.set(Obj, "color", `${info.dataHoursMinColor}`);
            Reflect.set(Obj, "height", pxToVw(info.dataHoursMinHeight));
            Reflect.set(Obj, "lineHeight", pxToVw(info.dataHoursMinHeight));
            Reflect.set(Obj, "borderRadius", pxToVw(14));
            Reflect.set(Obj, "width", pxToVw(info.dataHoursMinWidth));
            Reflect.set(Obj, "text-align", "center");
            break;
          case "viewMode-8-Date":
            Reflect.set(Obj, "backgroundColor", `${info.dataHoursMinBack}`);
            Reflect.set(Obj, "color", `${info.dataHoursMinColor}`);
            Reflect.set(Obj, "height", pxToVw(info.dataHoursMinHeight));
            Reflect.set(Obj, "lineHeight", pxToVw(info.dataHoursMinHeight));
            Reflect.set(Obj, "borderRadius", pxToVw(info.timeBorderRadius));
            Reflect.set(Obj, "width", pxToVw(info.dataHoursMinWidth));
            Reflect.set(Obj, "text-align", "center");
            break;
        }
        return Obj;
      };
    },

    //获取LOGO资源
    pictureResource() {
      let { imageUrl } = this.optionsInfo;
      if (this.$lodash.isObject(imageUrl)) {
        return imageUrl?.val?.url || imageUrl?.url;
      } else {
        return imageUrl;
      }
    },
    // 监听详情
    deptShown() {
      let templateInfo = localStorage.getItem("templateInfo");
      try {
        let info = JSON.parse(templateInfo);
        let resolutionArr = this.$lodash.split(info.resolution, "*");
        if (resolutionArr?.length && resolutionArr.length >= 2) {
          return resolutionArr[0] > resolutionArr[1];
        }
      } catch (err) {
        console.log("数据异常");
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDeptInfo"];
      console.log("roomDoctorInfo", roomDoctorInfo);
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },
    getShowInfo() {
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDeptInfo"];
      console.log("roomDoctorInfo", roomDoctorInfo);
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
      return this.optionsInfo.content || this.headTitle;
    },
    // 获取等候患者
    waitingPatientSummaryInfos() {
      let devType = this.devType;
      let WaitingPatientList = this.$store.getters["getWaitingPatienttAllInfo"];
      if (devType) {
        return WaitingPatientList.apiData;
      } else {
        return WaitingPatientList.mockData;
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.updateTime();
    this.handleOnMounted();
    console.log("this.", this.optionsInfo);
  },

  methods: {
    // 获取时间
    updateTime(val) {
      let timeType = this.optionsInfo.timeValue;
      if (!!val) {
        let msg = JSON.parse(val);
        if (!!msg && !!msg.timestamp) {
          let timestamp = dayjs.unix(msg.timestamp);
          [this.week, this.date, this.time] = dayjs(timestamp)
            .format(`d,${timeType},HH:mm`)
            .split(",");
        } else {
          [this.week, this.date, this.time] = dayjs()
            .format(`d,${timeType},HH:mm`)
            .split(",");
        }
      } else {
        [this.week, this.date, this.time] = dayjs()
          .format(`d,${timeType},HH:mm`)
          .split(",");
      }
    },

    handleResize() {
      console.log("拖动数据的变化");
    },

    handleChange(val, vmid) {
      console.log("val,vmid", val, vmid);
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      const self = this;
      // alert(key,JSON.stringify(data));
      switch (key) {
        case "headerInfo":
          self.updateTime(data);
          break;
      }
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = val.options.config;
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true,
    },

    designer: {
      handler: function (val) {
        const self = this;
        self.activation = val?.selectedId === this.widgetKey;
      },
      deep: true,
      immediate: true,
    },

    // 监听时间更新
    timeType: {
      handler: function (val) {
        if (val) {
          this.updateTime();
        }
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.scene-head-widget {
  padding: 0 30px;
  box-sizing: border-box;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  color: #333;
  height: 150px;
  width: 100%;
  overflow: hidden;
  position: relative;

  .left-con {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .adreess {
      width: 100%;
    }
  }
  .left-wrp {
    display: flex;
    align-items: center;
  }

  .item {
    flex: 1;
  }

  .split-area {
    flex: 3;
  }

  .logo-wrap {
    display: flex;
    width: 100%;
    overflow: hidden;
    align-items: center;

    img {
      height: 146px;
      max-height: 146px;
      max-width: 100%;
      object-fit: contain;
    }
  }

  .title {
    flex: 2;
    text-align: center;
    font-size: 80px;
    height: 100%;
    line-height: 1;
    box-sizing: border-box;
    align-self: center;
    //text-shadow: -1px 1px #BBB,
    //-2px 2px #BBB,
    //-3px 3px #BBB,
    //-4px 4px #BBB,
    //-5px 5px #BBB,
    //-7px 7px 3px #0008;
    align-items: center;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    :deep(.van-notice-bar) {
      background: transparent;
      color: black;
      height: 100%;
      width: 100%;
      // font-size: 68px;
      line-height: 90px;

      .van-notice-bar__wrap {
        height: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: center;

        > div {
          @include fontWeight();
        }
      }
    }

    .connectBoxLeft {
      padding: 0;
      height: 100%;

      :deep(.van-notice-bar__wrap) {
        height: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: left;

        > div {
          @include fontWeight();
        }
      }

      display: flex;
      justify-content: left;
      align-items: center;
    }

    .connectBoxCenter {
      height: 100%;
      display: flex;
      justify-content: left;
      align-items: center;

      :deep(.van-notice-bar__wrap) {
        height: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: center;

        > div {
          @include fontWeight();
        }
      }
    }
    .right-connectBoxCenter {
      .connectBoxCenter {
        height: 100%;
        display: block;
        justify-content: left;
        align-items: center;

        :deep(.van-notice-bar__wrap) {
          height: 100%;
          margin: 0 auto;
          display: flex;
          justify-content: center;

          > div {
            @include fontWeight();
          }
        }
      }
    }
    .connectBoxRight {
      height: 100%;
      display: flex;
      justify-content: left;
      align-items: center;

      :deep(.van-notice-bar__wrap) {
        height: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: right;

        > div {
          @include fontWeight();
        }
      }
    }
  }

  //视图一
  .viewMode-1 {
    overflow: hidden;
    line-height: 1;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;

    .time {
      float: right;
      margin-right: 8px;
      font-size: 90px;
      vertical-align: top;
      white-space: nowrap;
      box-sizing: border-box;
      align-self: center;
      // padding-right: 20px;
    }

    .sec {
      float: right;
      align-self: center;
      font-size: 32px;

      .week {
        text-align: right;
        white-space: nowrap;
      }

      .date {
        font-weight: 500;
        white-space: nowrap;
        box-sizing: border-box;
        margin-top: 10px;
      }
    }
  }

  //视图一:时间日期交换位置
  .date-time {
    .time {
      order: 1;
      padding-right: 0;
    }

    .sec {
      margin-right: 8px;
      padding-right: 20px;
    }
  }

  //视图二/三
  .viewMode-2,
  .viewMode-3 {
    font-size: 32px;
    display: flex;
    align-items: center;
    justify-content: right;

    .view-item {
      margin-right: 16px;
      box-sizing: border-box;

      &:last-child {
        margin-right: 0;
      }
    }
  }

  .viewMode-4 {
    font-size: 62px;
    display: flex;
    align-items: center;
    justify-content: right;

    .view-item {
      margin-right: 20px;
      box-sizing: border-box;

      &:last-child {
        margin-right: 0;
      }
    }
  }

  .viewMode-5 {
    display: flex;
    align-self: center;
    font-size: 42px;
    flex-direction: column;

    .right-top-area {
      float: right;
      line-height: 1.2;
      white-space: nowrap;
      box-sizing: border-box;
      align-self: flex-end;
    }

    .right-bottom-area {
      display: flex;
      font-size: 40px;
      line-height: 1.2;
      align-self: flex-end;
      flex-direction: row;

      .week,
      .date {
        white-space: nowrap;
      }
    }
  }
  .viewMode-6 {
    overflow: hidden;
    line-height: 1;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;

    .time {
      float: right;
      margin-right: 0;
      font-size: 90px;
      vertical-align: top;
      white-space: nowrap;
      box-sizing: border-box;
      align-self: center;
    }

    .sec {
      float: right;
      align-self: center;
      font-size: 32px;
      border-right: 4px solid #000;
      padding-right: 12px;
      .week {
        text-align: right;
        white-space: nowrap;
      }

      .date {
        font-weight: 500;
        white-space: nowrap;
        box-sizing: border-box;
        margin-top: 10px;
      }
    }
  }
  .viewMode-7 {
    display: flex;
    justify-content: flex-end;
    align-self: center;
    font-size: 42px;
    // flex-direction: column;
    //height: 120px;

    .title {
      background-color: rgba(22, 77, 135, 1);
      width: 666px;
      height: 120px;
      flex: 0 0 666px;
      line-height: 120px;
      color: rgba(255, 255, 255, 1);
      border-radius: 16px;
    }
  }
  .viewMode-8 {
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translate(-45%, -50%);
    .timeCenter {
      margin: auto;
      font-size: 90px;
      white-space: nowrap;
      box-sizing: border-box;
    }
  }
}

//面板
:deep(.first-split, .second-split) {
  height: 100%;
  width: 100%;
  overflow: hidden;
  outline: 1px solid #dcdee2;

  &:hover {
    outline: 1px solid #5cadff !important;
    box-sizing: border-box;

    .ivu-split-trigger-con {
      display: block !important;

      .ivu-split-trigger-vertical {
        background: #5cadff;
      }

      .ivu-split-trigger-bar {
        background: white;
      }
    }
  }

  .first-split-pane {
    height: 100%;
    padding: 10px;
    overflow: hidden;
  }

  .first-split-pane.no-padding {
    height: 200px;
    padding: 0;
  }

  .ivu-split-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
  }

  .ivu-split-pane {
    position: absolute;
  }

  .ivu-split-pane.left-pane,
  .ivu-split-pane.right-pane {
    top: 0;
    bottom: 0;
  }

  .ivu-split-pane.left-pane {
    left: 0;
  }

  .ivu-split-pane.right-pane {
    right: 0;
  }

  .ivu-split-pane.bottom-pane,
  .ivu-split-pane.top-pane {
    left: 0;
    right: 0;
  }

  .ivu-split-pane.top-pane {
    top: 0;
  }

  .ivu-split-pane.bottom-pane {
    bottom: 0;
  }

  .ivu-split-pane-moving {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  .ivu-split-trigger {
    border: 1px solid #dcdee2;
  }

  .ivu-split-trigger-con {
    position: absolute;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    z-index: 10;
    display: none !important;
  }

  .ivu-split-trigger-bar-con {
    position: absolute;
    overflow: hidden;
  }

  .ivu-split-trigger-bar-con.vertical {
    left: 1px;
    top: 50%;
    height: 32px;
    -webkit-transform: translate(0, -50%);
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%);
  }

  .ivu-split-trigger-bar-con.horizontal {
    left: 50%;
    top: 1px;
    width: 32px;
    -webkit-transform: translate(-50%, 0);
    -ms-transform: translate(-50%, 0);
    transform: translate(-50%, 0);
  }

  .ivu-split-trigger-vertical {
    width: 6px;
    height: 100%;
    background: #f8f8f9;
    border-top: none;
    border-bottom: none;
    cursor: col-resize;
  }

  .ivu-split-trigger-vertical .ivu-split-trigger-bar {
    width: 4px;
    height: 1px;
    background: rgba(23, 35, 61, 0.25);
    float: left;
    margin-top: 3px;
  }

  .ivu-split-trigger-horizontal {
    height: 6px;
    width: 100%;
    background: #f8f8f9;
    border-left: none;
    border-right: none;
    cursor: row-resize;
  }

  .ivu-split-trigger-horizontal .ivu-split-trigger-bar {
    height: 4px;
    width: 1px;
    background: rgba(23, 35, 61, 0.25);
    float: left;
    margin-right: 3px;
  }

  .ivu-split-horizontal > .ivu-split-trigger-con {
    top: 50%;
    height: 100%;
    width: 0;
  }

  .ivu-split-vertical > .ivu-split-trigger-con {
    left: 50%;
    height: 0;
    width: 100%;
  }

  .ivu-split .no-select {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
}

:deep(.split-area-pro) {
  outline: none !important;

  .ivu-split-trigger-con {
    display: none;
  }
}

.split-drag-area {
  width: 100%;
  height: 100%;
}
</style>
